package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.DataTianChuangBlacklistReqTO;
import com.handydata.antifraud.to.dap.DataTianChuangBlacklistResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 天创黑名单
 */
@Slf4j
@Service("dataTianChuangBlacklistWsServiceImpl")
@Comment("天创黑名单")
public class DataTianChuangBlacklistWsServiceImpl extends DataPlatformWsService<DataTianChuangBlacklistReqTO, DataTianChuangBlacklistResTO>
	implements WsInvocationService<DataTianChuangBlacklistReqTO, DataTianChuangBlacklistResTO> {

	@Override
	public DataTianChuangBlacklistResTO query(DataTianChuangBlacklistReqTO req) throws WsInvokeException {
		log.info("天创黑名单请求参数 {}", req);
		DataTianChuangBlacklistResTO resTO = new DataTianChuangBlacklistResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("天创黑名单参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("天创黑名单接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("天创黑名单参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("天创黑名单接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("天创黑名单参数校验 {}", "phone为空");
			resTO.setResCode("1111");
			resTO.setResMsg("天创黑名单接口phone参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.DATA_TIAN_CHUANG_BLACK_LIST.getCode();
		resTO = executeQuery(req);
		return resTO;
	}

	@Override
	protected String createQueryXml(DataTianChuangBlacklistReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>300640</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<name>").append(req.getName()).append("</name>");
		xml.append("<cid>").append(req.getIdCard()).append("</cid>");
		xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
		xml.append("</subatm>");
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected DataTianChuangBlacklistResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用天创黑名单接口返回报文：{}", resultXml);
		DataTianChuangBlacklistResTO resTO = new DataTianChuangBlacklistResTO();
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if ("0000".equals(jasonObject.getString("resCode"))) {
    			JSONObject resData = jasonObject.getJSONObject("resData");
    			if(resData != null) {
    				resTO.setBlackLevel(resData.getString("black_level"));
    			}
			} 
			resTO.setResCode((String)jasonObject.get("resCode"));
			resTO.setResMsg((String)jasonObject.get("resMsg"));
		} catch (Exception e) {
			log.info("调用天创黑名单接口异常：{}", e.getMessage());
			resTO.setResCode("1111");
			resTO.setResMsg("调用天创黑名单接口异常");
		}
		log.info("调用天创黑名单接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, DataTianChuangBlacklistReqTO.class);
	}
    
}


